package com.neurosky.thinkgear;

import android.os.Handler;
import android.os.Message;
import java.util.Arrays;
import java.util.List;

public class RespiratoryRate$CalcThread
  extends Thread
{
  private Handler a;
  
  public RespiratoryRate$CalcThread(RespiratoryRate paramRespiratoryRate, Handler paramHandler)
  {
    this.a = paramHandler;
  }
  
  public void run()
  {
    calculate();
  }
  
  public float calculate()
  {
    Arrays.fill(RespiratoryRate.a(this.b), 0.0F);
    RespiratoryRate.a(this.b, 0);
    while (RespiratoryRate.b(this.b) < RespiratoryRate.c(this.b))
    {
      RespiratoryRate.b(this.b, 0);
      while (RespiratoryRate.e(this.b) < RespiratoryRate.f(this.b).length)
      {
        RespiratoryRate.a(this.b)[RespiratoryRate.b(this.b)] += RespiratoryRate.f(this.b)[RespiratoryRate.e(this.b)] * RespiratoryRate.h(this.b)[(RespiratoryRate.b(this.b) + RespiratoryRate.e(this.b))];
        RespiratoryRate.g(this.b);
      }
      RespiratoryRate.d(this.b);
    }
    RespiratoryRate.i(this.b).clear();
    RespiratoryRate.j(this.b).Reset();
    RespiratoryRate.a(this.b, 0);
    while (RespiratoryRate.b(this.b) < RespiratoryRate.c(this.b))
    {
      RespiratoryRate.c(this.b, RespiratoryRate.j(this.b).AddData((int)RespiratoryRate.a(this.b)[RespiratoryRate.b(this.b)]));
      if (RespiratoryRate.k(this.b) > 0) {
        RespiratoryRate.i(this.b).add(Integer.valueOf(RespiratoryRate.b(this.b)));
      }
      RespiratoryRate.d(this.b);
    }
    RespiratoryRate.l(this.b).clear();
    for (int i = 0; i < RespiratoryRate.i(this.b).size(); i++)
    {
      RespiratoryRate.d(this.b, ((Integer)RespiratoryRate.i(this.b).get(i)).intValue() - 15);
      if (RespiratoryRate.m(this.b) < 0) {
        RespiratoryRate.d(this.b, 0);
      }
      RespiratoryRate.e(this.b, ((Integer)RespiratoryRate.i(this.b).get(i)).intValue() + 15);
      if (RespiratoryRate.n(this.b) > RespiratoryRate.a(this.b).length - 1) {
        RespiratoryRate.e(this.b, RespiratoryRate.a(this.b).length - 1);
      }
      RespiratoryRate.a(this.b, RespiratoryRate.a(this.b)[RespiratoryRate.m(this.b)]);
      for (int j = RespiratoryRate.m(this.b); j <= RespiratoryRate.n(this.b); j++) {
        if (RespiratoryRate.a(this.b)[j] >= RespiratoryRate.o(this.b))
        {
          RespiratoryRate.a(this.b, RespiratoryRate.a(this.b)[j]);
          RespiratoryRate.f(this.b, j);
        }
      }
      RespiratoryRate.l(this.b).add(Integer.valueOf(RespiratoryRate.p(this.b)));
    }
    if (RespiratoryRate.l(this.b).size() < 25) {
      return -1.0F;
    }
    RespiratoryRate.q(this.b).clear();
    RespiratoryRate.a(this.b, 1);
    while (RespiratoryRate.b(this.b) < RespiratoryRate.l(this.b).size())
    {
      RespiratoryRate.q(this.b).add(Integer.valueOf(((Integer)RespiratoryRate.l(this.b).get(RespiratoryRate.b(this.b))).intValue() - ((Integer)RespiratoryRate.l(this.b).get(RespiratoryRate.b(this.b) - 1)).intValue()));
      RespiratoryRate.d(this.b);
    }
    RespiratoryRate.l(this.b).remove(0);
    RespiratoryRate.a(this.b, new int[RespiratoryRate.q(this.b).size()]);
    for (i = 0; i < RespiratoryRate.q(this.b).size(); i++) {
      RespiratoryRate.r(this.b)[i] = ((Integer)RespiratoryRate.q(this.b).get(i)).intValue();
    }
    RespiratoryRate.a(this.b, 7);
    while (RespiratoryRate.b(this.b) < RespiratoryRate.q(this.b).size() - 1)
    {
      RespiratoryRate.a(this.b, new float[] { ((Integer)RespiratoryRate.q(this.b).get(RespiratoryRate.b(this.b) - 7)).intValue(), ((Integer)RespiratoryRate.q(this.b).get(RespiratoryRate.b(this.b) - 6)).intValue(), ((Integer)RespiratoryRate.q(this.b).get(RespiratoryRate.b(this.b) - 5)).intValue(), ((Integer)RespiratoryRate.q(this.b).get(RespiratoryRate.b(this.b) - 4)).intValue(), ((Integer)RespiratoryRate.q(this.b).get(RespiratoryRate.b(this.b) - 3)).intValue(), ((Integer)RespiratoryRate.q(this.b).get(RespiratoryRate.b(this.b) - 2)).intValue(), ((Integer)RespiratoryRate.q(this.b).get(RespiratoryRate.b(this.b) - 1)).intValue() });
      if ((((Integer)RespiratoryRate.q(this.b).get(RespiratoryRate.b(this.b))).intValue() > 1.4D * RespiratoryRate.b(this.b, RespiratoryRate.s(this.b))) || (((Integer)RespiratoryRate.q(this.b).get(RespiratoryRate.b(this.b))).intValue() < 0.6D * RespiratoryRate.b(this.b, RespiratoryRate.s(this.b)))) {
        RespiratoryRate.r(this.b)[RespiratoryRate.b(this.b)] = ((int)((((Integer)RespiratoryRate.q(this.b).get(RespiratoryRate.b(this.b) + 1)).intValue() + ((Integer)RespiratoryRate.q(this.b).get(RespiratoryRate.b(this.b) - 1)).intValue()) / 2.0D));
      }
      RespiratoryRate.d(this.b);
    }
    RespiratoryRate.q(this.b).clear();
    RespiratoryRate.a(this.b, 0);
    while (RespiratoryRate.b(this.b) < RespiratoryRate.r(this.b).length)
    {
      RespiratoryRate.q(this.b).add(Integer.valueOf(RespiratoryRate.r(this.b)[RespiratoryRate.b(this.b)]));
      RespiratoryRate.d(this.b);
    }
    RespiratoryRate.q(this.b).remove(0);
    RespiratoryRate.q(this.b).remove(1);
    RespiratoryRate.q(this.b).remove(2);
    RespiratoryRate.q(this.b).remove(3);
    RespiratoryRate.q(this.b).remove(4);
    RespiratoryRate.q(this.b).remove(5);
    RespiratoryRate.q(this.b).remove(6);
    RespiratoryRate.q(this.b).remove(RespiratoryRate.q(this.b).size() - 1);
    RespiratoryRate.l(this.b).remove(0);
    RespiratoryRate.l(this.b).remove(1);
    RespiratoryRate.l(this.b).remove(2);
    RespiratoryRate.l(this.b).remove(3);
    RespiratoryRate.l(this.b).remove(4);
    RespiratoryRate.l(this.b).remove(5);
    RespiratoryRate.l(this.b).remove(6);
    RespiratoryRate.l(this.b).remove(7);
    RespiratoryRate.l(this.b).remove(RespiratoryRate.l(this.b).size() - 1);
    RespiratoryRate.g(this.b, (int)(Math.floor(((Integer)RespiratoryRate.l(this.b).get(0)).intValue() / 64.0D) * 64.0D));
    RespiratoryRate.h(this.b, (int)(Math.ceil(((Integer)RespiratoryRate.l(this.b).get(RespiratoryRate.l(this.b).size() - 1)).intValue() / 64.0D) * 64.0D));
    RespiratoryRate.b(this.b, new int[RespiratoryRate.t(this.b) / 64 - RespiratoryRate.u(this.b) / 64 + 1]);
    RespiratoryRate.v(this.b)[0] = RespiratoryRate.u(this.b);
    RespiratoryRate.a(this.b, 1);
    while (RespiratoryRate.b(this.b) < RespiratoryRate.v(this.b).length)
    {
      RespiratoryRate.v(this.b)[RespiratoryRate.b(this.b)] = (RespiratoryRate.v(this.b)[(RespiratoryRate.b(this.b) - 1)] + 64);
      RespiratoryRate.d(this.b);
    }
    RespiratoryRate.c(this.b, RespiratoryRate.w(this.b).interpolate(RespiratoryRate.l(this.b), RespiratoryRate.q(this.b), RespiratoryRate.v(this.b)));
    if (RespiratoryRate.x(this.b).length < 256) {
      return -1.0F;
    }
    RespiratoryRate.c(this.b, RespiratoryRate.y(this.b).removeLinearTrend(RespiratoryRate.x(this.b)));
    RespiratoryRate.a(this.b, RespiratoryRate.z(this.b).pWelch(RespiratoryRate.x(this.b)));
    RespiratoryRate.d(this.b, RespiratoryRate.A(this.b).getPower());
    RespiratoryRate.e(this.b, RespiratoryRate.A(this.b).getFrequency());
    RespiratoryRate.a(this.b, 0);
    while (RespiratoryRate.b(this.b) < RespiratoryRate.B(this.b).length)
    {
      RespiratoryRate.B(this.b)[RespiratoryRate.b(this.b)] *= RespiratoryRate.C(this.b)[RespiratoryRate.b(this.b)];
      RespiratoryRate.d(this.b);
    }
    RespiratoryRate.b(this.b, 0.0F);
    RespiratoryRate.i(this.b, 6);
    RespiratoryRate.a(this.b, 6);
    while (RespiratoryRate.b(this.b) < 34)
    {
      if ((RespiratoryRate.B(this.b)[(RespiratoryRate.b(this.b) - 1)] < RespiratoryRate.B(this.b)[RespiratoryRate.b(this.b)]) && (RespiratoryRate.B(this.b)[(RespiratoryRate.b(this.b) + 1)] < RespiratoryRate.B(this.b)[RespiratoryRate.b(this.b)]) && (RespiratoryRate.B(this.b)[RespiratoryRate.b(this.b)] >= RespiratoryRate.D(this.b)))
      {
        RespiratoryRate.b(this.b, RespiratoryRate.B(this.b)[RespiratoryRate.b(this.b)]);
        RespiratoryRate.i(this.b, RespiratoryRate.b(this.b));
      }
      RespiratoryRate.d(this.b);
    }
    RespiratoryRate.c(this.b, (float)(Math.floor(RespiratoryRate.E(this.b)[RespiratoryRate.F(this.b)] * 60.0D * 100.0D + 0.5D) / 100.0D));
    this.a.obtainMessage(25, Float.valueOf(RespiratoryRate.G(this.b))).sendToTarget();
    return RespiratoryRate.G(this.b);
  }
}
